package com.example.ssu_eduapp.page;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.example.ssu_eduapp.eventlistener.ConnectionClosedReceiverListener;
import com.example.ssu_eduapp.eventlistener.ConnectionLoggedInReceiverListener;
import com.example.ssu_eduapp.eventlistener.SsuEduappListenerManager;
import com.example.ssu_eduapp.receiver.EventReceiver;
import com.example.ssu_eduapp.tools.BasePreference;
import com.example.ssu_eduapp.tools.Constants;
import com.example.ssu_eduapp.tools.LogUtils;

public class BaseFragmentActivity extends FragmentActivity implements ConnectionClosedReceiverListener, ConnectionLoggedInReceiverListener {

	private final String TAG = BaseFragmentActivity.class.getSimpleName();
	public LogUtils log = new LogUtils(TAG);
	
	public BasePreference bpref;
	
	private EventReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		log.i("onCreate");
		
		bpref 		= new BasePreference(this);
		
		receiver = new EventReceiver();
		setRegisterListener();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		log.i("onCreate");
//		setReceiver(new EventReceiver());
//		setRegisterListener();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		log.i("onPause");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		log.i("onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		log.i("onDestroy");
		
		
	}

	public void setPreference(String name, Object value) {
		log.i("setPreference");
		if (value instanceof String) {
			bpref.put(name, String.valueOf(value));
		} else if (value instanceof Integer) {
			bpref.put(name, Integer.parseInt(String.valueOf(value)));
		}

	}
	
	private void setRegisterListener(){
		
		SsuEduappListenerManager.setConnectionClosedReceiverListener(this);
		SsuEduappListenerManager.setConnectionLoggedInReceiverListenr(this);
		
		registerReceiver(getReceiver(), new IntentFilter(
				Constants.INTENT_FILTER_ACTION_CONNECTION_CLOSED));

		registerReceiver(getReceiver(), new IntentFilter(
				Constants.INTENT_FILTER_ACTION_LOGGED_IN));
		
	}
	
	private void unRegisterReceiver(){
		
		SsuEduappListenerManager.setConnectionClosedReceiverListener(null);
		SsuEduappListenerManager.setConnectionLoggedInReceiverListenr(null);
		
		unregisterReceiver(getReceiver());
		
	}
	
	
	public EventReceiver getReceiver(){
		log.i("getReceiver");
		return receiver;
	}
	public void setReceiver(EventReceiver receiver){
		log.i("setReceiver");
		this.receiver = receiver;
	}

	@Override
	public void connectionClosed() {
		// TODO Auto-generated method stub
	}
	@Override
	public void connectionLoggedIn() {
		// TODO Auto-generated method stub
	}
	
}